package com.junior.mybatisredis.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.junior.mybatisredis.config.RedisConfig;
import com.junior.mybatisredis.entity.Blog;
import com.junior.mybatisredis.mapper.BlogMapper;
import com.junior.mybatisredis.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String  findById(int id) {
        String key  = "Blog_"+id;
        ValueOperations<String ,String> operations = redisTemplate.opsForValue();
        //判断缓存是否存在
        Boolean key1 = redisTemplate.hasKey(key);
        if(key1){
            String blog = operations.get(key);
            System.out.println("从缓存中获取值,剩余时间为"+redisTemplate.getExpire(key));
            //取出为json字符串
            return blog;
        }
        Blog blog = blogMapper.findById(id);
        //将blog对象转换为json字符串
        String blogJson = JSON.toJSONString(blog);
        System.out.println("从数据库中获取值");
        //value为json字符串
        operations.set(key,blogJson,600, TimeUnit.SECONDS);
        return blogJson;
    }

    @Override
    public List<String> findByAuthor(String author) {
        String key = "Blog_"+author;
        ListOperations<String,List> operations = redisTemplate.opsForList();
        Boolean hasKey = redisTemplate.hasKey(key);
        if(hasKey){
            List blogs = operations.range(key, 0,-1);
            System.out.println("从缓存中获取list，剩余时间"+redisTemplate.getExpire(key));
            return (List<String>) blogs.get(0);
        }
        List<String > blogs = new ArrayList<>();
        System.out.println("从数据库从取值");
        List<Blog> blogList = blogMapper.findByAuthor(author);
        for (Blog blog : blogList) {
            String s = JSON.toJSONString(blog);
            blogs.add(s);
        }
        operations.rightPush(key,blogs);
        redisTemplate.expire(key,600,TimeUnit.SECONDS);
        return blogs;
    }

    @Override
    public List<Blog> findAll() {
        List<Blog> blogs = blogMapper.findAll();
        return blogs;
    }

    @Override
    public int addOne(Map map) {
        try {
            blogMapper.addOne(map);
        }
        catch (Exception e){
            System.out.println(e);
            return 0;
        }
        return 1;
    }

    @Override
    public int deleteOne(int id) {
        try {
            String key = "Blog_"+id;
            //根据key获取缓存的author，并判断author是否存在于缓存
            ValueOperations<String, String> operations = redisTemplate.opsForValue();
            String s = operations.get(key);
            JSONObject jsonObject = JSON.parseObject(s);
            Object author = jsonObject.get("author");
            String key1 = "Blog_"+author;
            Boolean hasKey = redisTemplate.hasKey(key1);
            if(hasKey){
                redisTemplate.delete(key1);
                System.out.println("执行删除操作，从缓存中删除key_list");
            }
            Boolean keyBloolen = redisTemplate.hasKey(key);
            //是否有id缓存
            if(keyBloolen){
                redisTemplate.delete(key);
                System.out.println("执行删除操作，从缓存中删除key");
            }
            blogMapper.deleteOne(id);
        }
        catch (Exception e){
            System.out.println(e);
            return 0;
        }
        return 1;
    }

    @Override
    public int updateById(Blog blog) {
        try {
            String key =  "Blog_"+blog.getId();
            String key1 =  "Blog_"+blog.getAuthor();
            Boolean hasKey = redisTemplate.hasKey(key);
            Boolean hasKey1 = redisTemplate.hasKey(key1);
            if(hasKey){
                redisTemplate.delete(key);
                System.out.println("执行更新操作，将缓存key删除");
            }
            if(hasKey1){
                redisTemplate.delete(key1);
                System.out.println("执行更新操作，将缓存key_list删除");
            }
            blogMapper.updateById(blog);
        }catch (Exception e){
            System.out.println(e);
            return 0;
        }
        return 1;
    }
}
